package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Unitdoor;
import com.service.UnitdoorService;

@Controller
@RequestMapping("/unitdoor")
public class UnitdoorController {

	@Resource
	UnitdoorService unitdoorService;
	
	@RequestMapping(value={"/show"}, produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String show(HttpServletRequest request,Unitdoor unitdoor,ModelMap map) throws Exception{
		String s1=request.getParameter("address");
		String address = new String(s1.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println("--------"+address);
		Map<String,Object> maps=new HashMap<String,Object>();
		maps.put("address", address);
		maps.put("number", "all");
		List<Unitdoor> list=unitdoorService.showAll(maps);
		 JsonConfig config = new JsonConfig();
		 config.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if (arg1.equals("shoppingCarts")) {
					 return true;
				}else{
					return false;
				}
			}
		});
		map.addAttribute("list", list);
		JSONArray json=JSONArray.fromObject(list, config);
		System.out.println(json);
		return json.toString();
	}
	
	@RequestMapping(value="/showAll")
	public String showAll(ModelMap map){
		List<Unitdoor> unitdoor=unitdoorService.showUnitdoor();
		map.addAttribute("unitdoor", unitdoor);
		return "Unitdoor";
	}
	
	@RequestMapping(value="/selectOne")
	public String selectOne(ModelMap map,String id){
		Unitdoor unitdoor=unitdoorService.selectOne(id);
		map.addAttribute("unitdoor", unitdoor);
		return "showUnitdoor";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Unitdoor unitdoor){
		unitdoor.setYid(unitdoor.getYid());
		unitdoor.setNumber(unitdoor.getNumber());
		unitdoor.setAddress(unitdoor.getAddress());
		unitdoor.setIdnumber(unitdoor.getIdnumber());
		unitdoorService.update(unitdoor);
		return "redirect:/unitdoor/showAll";
	}
	
	@RequestMapping(value="insert", method=RequestMethod.POST)
	public String insert(Unitdoor unitdoor){
		unitdoor.setNumber(unitdoor.getNumber());
		unitdoor.setAddress(unitdoor.getAddress());
		unitdoor.setIdnumber(unitdoor.getIdnumber());
		unitdoorService.insert(unitdoor);
		return "redirect:/unitdoor/showAll";
	}
}

